//
//  sync_channel.hpp
//  Amqp_test
//
//  Created by HNXF on 2022/7/28.
//

#ifndef sync_channel_hpp
#define sync_channel_hpp

#include <amqpcpp.h>
#include <boost/asio/io_service.hpp>
#include <condition_variable>
#include <mutex>

class sync_channel {
public:
    sync_channel(boost::asio::io_service &io_service, AMQP::Connection &connect);
    ~sync_channel();
    
    void stop();
    
    void publish(const std::string& topic,
        const std::string& partition_key,
        const std::string& event_type_name,
        const std::string& message);
private:
    void set_envelope_format(const std::string& event_type_name, AMQP::Envelope& envelope);
    void wait_operation_finish(std::unique_lock<std::recursive_mutex> lock);
    void on_error(const std::string& message);
    void on_success();
private:
    boost::asio::io_service& io_service_;
    AMQP::Channel channel_;
    AMQP::Reliable<> reliable_;
    
    std::mutex publish_mutex_;
    std::recursive_mutex operation_mutex_;
    std::condition_variable_any operation_finished_cv_;

    bool operation_finished_;
    bool is_error_state_;
    std::string error_message_;
};
#endif /* sync_channel_hpp */
